package com.etl.dataflow.executor.communication.controller;

import cn.hutool.json.JSONUtil;
import com.etl.dataflow.common.entity.TaskInfo;
import com.etl.dataflow.common.response.R;
import com.etl.dataflow.executor.communication.form.BatchTaskStatisticForm;
import com.etl.dataflow.executor.communication.service.ExecutorService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * 执行器向调度中心暴露的接口
 *
 * @author dx
 * @since 2022/4/20
 */
@RestController
public class ExecutorController {

    @Resource
    private ExecutorService executorService;

    @GetMapping("/ping")
    public R ping() {
        return R.success();
    }

    @GetMapping("/jvmInfo")
    public R executorJvmInfo() {
        return executorService.executorJvmInfo();
    }

    @GetMapping("/getRuntimeInfo")
    public R getHwInfo() {
        return executorService.getExecutorRuntimeInfo();
    }

    @PostMapping("/task/run")
    public R run(@RequestBody @Validated TaskInfo taskInfo) {
        System.out.println(JSONUtil.toJsonStr(JSONUtil.parse(taskInfo), 2));
        return executorService.runTask(taskInfo);
    }

    @GetMapping("/task/terminate/{taskId}")
    public R terminate(@NotBlank @Validated @PathVariable String taskId) {
        return executorService.terminateTask(taskId);
    }

    @GetMapping("/task/statistic")
    public R getTaskStatistic(@NotBlank @Validated String taskId) {
        return executorService.getTaskStatistic(taskId);
    }

    @PostMapping("/task/statistic/batch")
    public R getTaskStatistic(@RequestBody @Validated BatchTaskStatisticForm form) {
        return executorService.getTaskStatisticBatch(form);
    }

    @GetMapping("/task/running")
    public R getRunningTasks() {
        return executorService.getRunningTasks();
    }


}
